home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mm / mm-0.90 / dates.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-18  |  4.7 KB  |  218 lines

  1. /*
  2.  * Copyright (c) 1986, 1990 by The Trustees of Columbia University in
  3.  * the City of New York.  Permission is granted to any individual or
  4.  * institution to use, copy, or redistribute this software so long as it
  5.  * is not sold for profit, provided this copyright notice is retained.
  6.  */
  7.  
  8. #ifndef lint
  9. static char *rcsid = "$Header: /f/src2/encore.bin/cucca/mm/tarring-it-up/RCS/dates.c,v 2.1 90/10/04 18:23:52 melissa Exp $";
  10. #endif
  11.  
  12. /*
  13.  * dates.c - miscellaneous routines to manipulate date/time formats
  14.  */
  15.  
  16. #include "mm.h"
  17. #if BSD
  18. #include <sys/timeb.h>            /* XXX use gettimeofday */
  19. #endif
  20.  
  21. static int dayspermonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  22.  
  23. char *month_names[] = {
  24.     "January", "February", "March", "April", "May", "June", "July",
  25.     "August", "September", "October", "November", "December"
  26. };
  27. static char *day_names[] = {
  28.     "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
  29.     "Saturday"
  30. };
  31.  
  32. int minutes_west;            /* offset from gmt; set in init.c */
  33.  
  34. #define dysize(y) \
  35.     ((((y) % 4) == 0 && ((y) % 100) != 0 || ((y) % 400) == 0) ? 366 : 365)
  36.  
  37. time_t
  38. itime(t)
  39. struct tm *t;                /* as returned by localtime(3) */
  40. {
  41.     int i;
  42.     unsigned long x = 0;
  43.  
  44.     int dst = (t->tm_isdst ? 1 : 0);    /* how much to subtract for dst */
  45.  
  46.     /* count days in previous years back to 1970 */
  47.     for (i = (1900 + (t->tm_year) - 1); i >= 1970; i--)
  48.     x += dysize(i);
  49.  
  50.     /* add in days in previous months for this year */
  51.     for (i = t->tm_mon; i > 0; i--)
  52.     x += dayspermonth[i-1];
  53.  
  54.     /* include Feb 29th if it has occurred this year */
  55.     if ((dysize(t->tm_year) == 366) && (t->tm_mon >= 2))
  56.     x++;
  57.  
  58.     x += t->tm_mday - 1;        /* add days this month */
  59.  
  60.     x = (24*x) + t->tm_hour - dst;    /* convert and add hours */
  61.     x = (60*x) + t->tm_min + minutes_west; /* add minutes, convert to gmt */
  62.     x = (60*x) + t->tm_sec;        /* seconds */
  63.     return (time_t) x;
  64. }
  65.  
  66. /*
  67.  * 1 Jan 70
  68.  */
  69. char *
  70. cdate(it)
  71. time_t it;
  72. {
  73.     struct tm *t;
  74.     static char str[16];
  75.  
  76.     t = localtime(&it);
  77.     sprintf(str, "%d %3.3s %d", t->tm_mday, month_names[t->tm_mon],
  78.         (t->tm_year < 100) ? t->tm_year : t->tm_year + 1900);
  79.     return (char *) str;
  80. }
  81.  
  82. /*
  83.  * ctad - convert internal time to ascii
  84.  *
  85.  * Similar to ctime, but the output format is "11 Oct 86 12:42am"
  86.  * Returns current time if the argument is zero.
  87.  */
  88.  
  89. char *
  90. ctad(it)
  91. time_t it;
  92. {
  93.     struct tm *t;
  94.     static char str[32];
  95.  
  96.     if (it == 0)
  97.     (void) time (&it);
  98.  
  99.     t = localtime(&it);
  100.     sprintf(str,"%d %3.3s %d %d:%02d%s",
  101.         t->tm_mday, month_names[t->tm_mon],
  102.         (t->tm_year < 100) ? t->tm_year : t->tm_year + 1900,
  103.         (((t->tm_hour % 12) == 0) ? 12 : (t->tm_hour % 12)),
  104.         t->tm_min, ((t->tm_hour < 12) ? "am" : "pm"));
  105.     return (char *) str;
  106. }
  107.  
  108. /*
  109.  * fdate - convert internal date/time to message separator format used
  110.  * in mail files
  111.  *
  112.  * 01-Jan-70 00:00:00-GMT
  113.  */
  114.  
  115. char *
  116. fdate (it)
  117. time_t it;
  118. {
  119.     struct tm *t;
  120.     static char str[32];
  121.  
  122.     t = gmtime (&it);
  123.     sprintf (str, "%2d-%3.3s-%d %2d:%02d:%02d-GMT",
  124.          t->tm_mday, month_names[t->tm_mon],
  125.          (t->tm_year < 100) ? t->tm_year : t->tm_year + 1900,
  126.          t->tm_hour, t->tm_min, t->tm_sec);
  127.     return (char *) str;
  128. }
  129.  
  130. /*
  131.  * hdate - convert internal date to ascii
  132.  *
  133.  * return a date string for use in displaying headers in the form
  134.  * nn-mmm
  135.  */
  136.  
  137. char *
  138. hdate (it)
  139. time_t it;
  140. {
  141.     struct tm *t;
  142.     static char str[32];
  143.     extern int thisyear;
  144.  
  145.     t = localtime(&it);
  146.     /* %2.0d works.  it should be %2.2d, but that comes out as "07" */
  147.     sprintf(str,"%2.0d-%3.3s",
  148.         t->tm_mday, month_names[t->tm_mon]);
  149.     return (char *) str;
  150. }
  151.  
  152.  
  153. /*
  154.  * More verbose version of ctad; format is "Thursday, 31 July 1986 11:20PM"
  155.  *
  156.  * If the argument is zero, the current date is returned.
  157.  */
  158. char *
  159. daytime(it)
  160. time_t it;
  161. {
  162.     struct tm *t;
  163.     static char str[64];
  164.  
  165.     if (it == 0)
  166.     (void) time (&it);
  167.  
  168.     t = localtime(&it);
  169.     sprintf(str,"%s, %d %s %d %d:%02d%s",
  170.         day_names[t->tm_wday],
  171.         t->tm_mday, month_names[t->tm_mon], t->tm_year + 1900,
  172.         (((t->tm_hour % 12) == 0) ? 12 : (t->tm_hour % 12)),
  173.         t->tm_min, ((t->tm_hour < 12) ? "AM" : "PM"));
  174.     return (char *) str;
  175. }
  176.  
  177.  
  178. /*
  179.  * rfc822-acceptable date, like:
  180.  * Thu, 1 Jan 70 00:00:00 GMT
  181.  */
  182. char *
  183. rfctime(it)
  184. time_t it;
  185. {
  186.     struct tm *t,t1;
  187.     static char str[64];
  188. #if BSD
  189.     struct timeb tb;
  190. #endif
  191. #if SYSV
  192.     extern char *tzname[2];
  193. #endif
  194.  
  195.     if (it == 0)
  196.     (void) time (&it);
  197.  
  198.     t = localtime(&it);
  199. #if BSD
  200.     ftime (&tb);
  201. #endif
  202. #if SYSV
  203.     asctime(t);
  204. #endif    
  205.     sprintf(str,"%3.3s, %d %3.3s %d %d:%02d:%02d %s",
  206.         day_names[t->tm_wday],
  207.         t->tm_mday, month_names[t->tm_mon], t->tm_year /*+ 1900*/,
  208.         t->tm_hour, t->tm_min, t->tm_sec,
  209. #if BSD
  210.         timezone(tb.timezone, t->tm_isdst)
  211. #endif
  212. #if SYSV
  213.         tzname[t->tm_isdst ? 1 : 0]
  214. #endif
  215.         );
  216.     return (char *) str;
  217. }
  218.